#include <iostream>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include<sys/time.h>
#include<sys/types.h>


// 设置文件描述符为非阻塞状态
void SetNonBlock(int fd)
{
    // 获取文件描述符的状态
    int fl = fcntl(fd, F_GETFL);
    if (fl < 0)
    {
        std::cout << "fcntl erro" <<" "<<std::endl;
    }

    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}

int main()
{
    std::cout<<sizeof(fd_set)<<std::endl;
    // 将0号文件描述符设为非阻塞状态
    SetNonBlock(0);
    char buffer[1024];
    while (1)
    {
        ssize_t s = read(0, buffer, sizeof(buffer) - 1);
        if (s > 0)
        {
            buffer[s] = 0;
            write(1, buffer, strlen(buffer));
            std::cout << "read success!" << s << std::endl;
        }
        else
        {
            if (errno == 11 )
            {
                // 读取失败
                std::cout << "read failed,文件描述符未就绪！" << errno << std::endl;
                sleep(1);
                continue;
            }
        }
    }
    std::cout<<sizeof(fd_set)<<std::endl;
    return 0;
}
